/**
 * Copyright (c) 2016-2019 九点科技 All rights reserved.
 *
 * https://www.9dyun.cn
 *
 * 版权所有，侵权必究！
 */

package com.dian.common.controller;


import cn.hutool.core.util.StrUtil;
import com.dian.common.annotation.SysLog;
import com.dian.common.utils.EnumUtil;
import com.dian.common.utils.R;
import com.dian.modules.base.entity.BaseConfigEntity;
import com.dian.modules.base.entity.DistEntity;
import com.dian.modules.base.service.BaseConfigService;
import com.dian.modules.base.service.DistService;
import com.dian.modules.inv.service.GoodsAccountService;
import com.dian.modules.sys.entity.SysConfigEntity;
import com.dian.modules.sys.entity.SysDistEntity;
import com.dian.modules.sys.entity.UserTableEntity;
import com.dian.modules.sys.service.SysConfigService;
import com.dian.modules.sys.service.SysDistService;
import com.dian.modules.sys.service.UserTableService;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.*;

/**
 * 通用配置信息
 *
 * @author  xiaolai@9dyun.cn
 */
@RestController
@RequestMapping("/common")
public class CommonController extends AbstractController {
    @Autowired
    private UserTableService userTableService;
    @Autowired
    private DistService distService;

    //@Autowired
    //private SysDistService sysDistService;

    //@Autowired
    //private SysConfigService sysConfigService;

    @Autowired
    private BaseConfigService baseConfigService;



    /**
     * 业务数据字典列表
     */
    @GetMapping("/get-conf-dist")
    public R listAll(@RequestParam Map<String, Object> params){

        Collection baseDistlist = distService.queryCacheAll();

        //List<SysDistEntity> sysDistlist = sysDistService.queryCacheAll();


        //List<SysConfigEntity> sysConfigList = sysConfigService.queryCacheAll();

        Collection baseContfigList = baseConfigService.queryCacheAll();

        return R.ok().put("baseDistlist",baseDistlist)
                //.put("sysDistlist",sysDistlist)
                //.put("sysConfigList",sysConfigList)
                .put("baseContfigList",baseContfigList);
    }
    /**
     * 保存个性化表单列信息
     */
    @PostMapping(value = "/save-table-cols")
    public R saveTableCols(@RequestBody UserTableEntity tableEntity) {
        userTableService.updateInfo(tableEntity);

        return R.ok();
    }
	/*

	@RequestMapping(value = "/get-table-cols")
	public R getTableCols(@RequestParam Map<String, String> params) {
		return R.ok(userTableService.getInfo(params.get("tableKey")));
	}*/

    /**
     * 获取个性化表单列信息
     */

    @RequestMapping(value = "/get-table-cols")
    public R getTableCols() {
        return R.ok(userTableService.queryList());
    }
    /**
     * 类型,状态,各个枚举类型的javascript对象
     */
    @GetMapping(value = "/enums")
    public R enums() {
        List<Map<String,?>> list= Lists.newArrayList();
        EnumUtil.getEnumAllObject() .forEach((k, v) -> {
            Map<String,Object> map= Maps.newLinkedHashMap();
            List<Map<String,?>> enumList=Lists.newArrayList();
            v.forEach((k1,v1) -> {
                Map<String,String> enumMap= Maps.newLinkedHashMap();
                enumMap.put("key",String.valueOf(v1.getValue()));
                enumMap.put("value",v1.getDesc());
                enumList.add(enumMap);
            });
            String cName=k.getCanonicalName();
            String simpleName=cName.substring(23,cName.length());
            map.put("key",simpleName);
            map.put("value",enumList);
            list.add(map);
        });
        list.add(new HashMap<String, Object>(){{
            put("key","comm.ValidEnum");
            put("value",new ArrayList<HashMap<String,String>>() {{
                add(new HashMap<String, String>(){{
                    put("key","1");
                    put("value","是");
                }});
                add(new HashMap<String, String>(){{
                    put("key","0");
                    put("value","否");
                }});
            }});
        }});
        return R.ok(list);
    }
    /**
     * 库存表导出
     */
    @GetMapping("/temp-download")
    public void tempDownload(@RequestParam Map<String, Object> params){
        String tempName="";
        if(params.get("tempName")!=null){
            tempName=(String) params.get("tempName");
        }
        if(StrUtil.isBlank(tempName)){
            return ;
        }


        try {
            // 读到流中
            String filePath=String.format("%s%s%s","tmp", File.separator,tempName);
            File file=new File(filePath);
            if(!file.exists()||!file.isFile()){
                return ;
            }

            InputStream inStream = new FileInputStream(filePath);//文件的存放路径
            // 设置输出的格式
            this.getResponse().reset();
            this.getResponse().setHeader("content-Type", "application/vnd.ms-excel");
            this.getResponse().setHeader("Content-Disposition",
                    "attachment;filename=" + URLEncoder.encode(tempName, "UTF-8"));

            byte[] b = new byte[200];
            int len;
            while ((len = inStream.read(b)) > 0){
                this.getResponse().getOutputStream().write(b, 0, len);
            }
            inStream.close();
            file.delete();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
